package com.hzhc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.SysUserRole;
import com.hzhc.mapper.SysUserRoleMapper;
import com.hzhc.service.SysUserRoleService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.SysUserVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户与岗位/角色对应关系 服务实现类
 * </p>
 *
 * @author duanhm
 * @since 2019-07-26
 */
@Service
@RequiredArgsConstructor
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

    private final SysUserRoleMapper userRoleMapper;


    @Override
    public ResultUtil<T> saveUserRole(SysUserVo vo) {

        boolean flag = true;
        if (vo.getId() != null) {
            LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysUserRole::getUserId,vo.getId());
            int delete = userRoleMapper.delete(wrapper);

            String[] split = vo.getRoleIdss().split(",");
            for (String s : split) {
                SysUserRole ur = new SysUserRole();
                ur.setRoleId(Long.valueOf(s));
                ur.setUserId(Long.valueOf(vo.getId()));
                userRoleMapper.insert(ur);
            }
            flag = false;
        }
        if (flag){
            return ResultUtil.fail();
        }
        return ResultUtil.ok();
    }
}
